using System;
using System.Drawing;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmUserLogin : Form {
        private UserBLL _userBll = UserBLL.GetUserInfoBLL();
        public FrmUserLogin() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            new FrmManagerLogin().Show();
        }


        private void FrmUserLogin_Load(object sender, EventArgs e) {
            UpdateCode();
        }

        private void UpdateCode() {
            pictureBox1.Image = Code.GetCode();
        }

        private void pictureBox1_Click(object sender, EventArgs e) {
            UpdateCode();
        }

        //登录按钮
        private void button1_Click(object sender, EventArgs e) {
            //验证验证码
            if (!Code.CheckCode(textBox3.Text.Trim())) {
                label4.Text = "Captcha Code is incorrect.";
            }
            //将参数存入
            string name = textBox1.Text.Trim();
            string pwd = textBox2.Text.Trim();
            label4.ForeColor = Color.Red;
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd)) {
                label4.Text = "username or password is empty";
                return;
            }

            Result<User> result = _userBll.UserLogin(new User() {
                UserName = name,
                Password = pwd
            });

            if (!result.IsSuccess) {
                label4.Text = result.Message;
                return;
            }
            MessageBox.Show("login success");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}